package clientView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import clientUtilGUI.UtilityGUI;

public class Province_View extends JPanel implements ActionListener{
	
	private JPanel panelHeader;
	private JSplitPane splitPane;	
	private JPanel panelLeft;
	private GridBagLayout gbl_panelLeft;
	private JLabel lblProvince;
	private GridBagConstraints gbc_lblProvince;
	private static JPanel panelRight;
	private GridBagLayout gbl_panelRight;
	private JLabel lblQuanHuyen;
	private GridBagConstraints gbc_lblQuanHuyen;
	private static JLabel lblTen;
	private static GridBagConstraints gbc_lblId;
	public static JTable tblDis;
	private static GridBagConstraints gbc_scrollTblDis;
	public static JTable tblProV;
	public static JScrollPane scrollTblDis;
    private int pageCur = 1;
	public static  JScrollPane scrollPane;
	private JPanel panelPage;
	private JButton btnBack;
	private JComboBox cbbPage;
	private JButton btnNext;
	private JButton btnTp;
	private static JButton btnthem;
    public Province_View(){
    	setLayout(new BorderLayout(0, 0));
		
		panelHeader = UtilityGUI.headerForm("Danh Sách Đơn Vị Hành Chính");
		add(panelHeader, BorderLayout.NORTH);
		
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
	    splitPane.setContinuousLayout(true);
	    splitPane.setOneTouchExpandable(true);	    	    
	    splitPane.setDividerLocation(430);
	    
	    /*--------------- Bang Tỉnh Thành Phố -----------------*/
	    panelLeft = new JPanel();
	    gbl_panelLeft = new GridBagLayout();
	    gbl_panelLeft.columnWidths = new int[]{0, 0, 0};
	    gbl_panelLeft.rowHeights = new int[]{0, 0};
	    gbl_panelLeft.columnWeights = new double[]{0.0, 0.5, 0.5};
	    gbl_panelLeft.rowWeights = new double[]{0.0, 1.0};
	    panelLeft.setLayout(gbl_panelLeft);
	    
	    //	Title	    
	    lblProvince = new JLabel("Tỉnh-Thành Phố");
	    lblProvince.setFont(UtilityGUI.fontHeader2);
	    gbc_lblProvince = new GridBagConstraints();
	    gbc_lblProvince.anchor = GridBagConstraints.WEST;
	    gbc_lblProvince.insets = new Insets(5, 5, 5, 5);
	    gbc_lblProvince.gridx = 1;
	    gbc_lblProvince.gridy = 0;
	    panelLeft.add(lblProvince, gbc_lblProvince);
	    
	    //	Table
	    tblProV = UtilityGUI.getTable("Province", pageCur, "ProvinceId", true);
	    
		btnTp = new JButton("Thêm Tỉnh-Thành Phố");
		btnTp.setFont(UtilityGUI.fontBtnForm);
		btnTp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new Province_Insert(null).setVisible(true);
			}
		});
		GridBagConstraints gbc_btnThmTnhthnhPh = new GridBagConstraints();
		gbc_btnThmTnhthnhPh.insets = new Insets(0, 0, 5, 0);
		gbc_btnThmTnhthnhPh.gridx = 2;
		gbc_btnThmTnhthnhPh.gridy = 0;
		panelLeft.add(btnTp, gbc_btnThmTnhthnhPh);
		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 10, 5, 10);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		scrollPane.setViewportView(tblProV);
		panelLeft.add(scrollPane, gbc_scrollPane);
		
		panelPage = new JPanel();
		GridBagConstraints gbc_panelPage = new GridBagConstraints();
		gbc_panelPage.gridwidth = 3;
		gbc_panelPage.insets = new Insets(0, 10, 10, 10);
		gbc_panelPage.fill = GridBagConstraints.BOTH;
		gbc_panelPage.gridx = 0;
		gbc_panelPage.gridy = 2;
		panelLeft.add(panelPage, gbc_panelPage);
		panelPage.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnBack = new JButton(new ImageIcon("src/image/back.png"));
		btnBack.addActionListener(this);
		panelPage.add(btnBack);
		
		cbbPage = UtilityGUI.cbbPaging("Province");
		cbbPage.addActionListener(this);
		panelPage.add(cbbPage);
		
		btnNext = new JButton(new ImageIcon("src/image/next.png"));
		btnNext.addActionListener(this);
		panelPage.add(btnNext);
		  splitPane.setLeftComponent(panelLeft);
			this.add(splitPane, BorderLayout.CENTER);

	    //	Add Mouse Event	    
	    tblProV.addMouseListener(new MouseListPr());
	    
	    /*-------------- END Bang Tỉnh Tp --------------*/
	    
	    /*--------------- Bang Quận Huyện -----------------*/
	    panelRight = new JPanel();
	    gbl_panelRight = new GridBagLayout();
	    gbl_panelRight.columnWidths = new int[]{0, 0, 0, 0, 0};
	    gbl_panelRight.rowHeights = new int[]{0, 0};
	    gbl_panelRight.columnWeights = new double[]{0.0, 0.5, 0.0, 0.5, 0.0};
	    gbl_panelRight.rowWeights = new double[]{0.0, 1.0};
	    panelRight.setLayout(gbl_panelRight);
	    
	    lblQuanHuyen = new JLabel("Quận-Huyện :");
	    lblQuanHuyen.setFont(UtilityGUI.fontHeader2);
	    gbc_lblQuanHuyen = new GridBagConstraints();
	    gbc_lblQuanHuyen.anchor = GridBagConstraints.EAST;
	    gbc_lblQuanHuyen.insets = new Insets(5, 15, 5, 5);
	    gbc_lblQuanHuyen.gridx = 0;
	    gbc_lblQuanHuyen.gridy = 0;
	    panelRight.add(lblQuanHuyen, gbc_lblQuanHuyen);
	    
	    lblTen = new JLabel("");
	    lblTen.setFont(UtilityGUI.fontHeader2);
		lblTen.setForeground(UtilityGUI.colorHeader2);
	    gbc_lblId = new GridBagConstraints();
	    gbc_lblId.insets = new Insets(5, 0, 5, 5);
	    gbc_lblId.anchor = GridBagConstraints.WEST;
	    gbc_lblId.gridx = 1;
	    gbc_lblId.gridy = 0;
	    panelRight.add(lblTen, gbc_lblId);
	    
	    tblDis = UtilityGUI.createTblDistrict(100,1,"DistrictId",true);
	    
	    btnthem =  new JButton("Thêm Quận Huyện");
	    btnthem.setFont(UtilityGUI.fontBtnForm);
	    btnthem.setVisible(false);
	    btnthem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent arg0) {
	    	    int maT = (Integer) tblProV.getModel().getValueAt(tblProV.getSelectedRow(), 0);
	    		new District_Insert(null,maT).setVisible(true);
	    	}
	    });
	    GridBagConstraints gbc_btnthem = new GridBagConstraints();
	    gbc_btnthem.anchor = GridBagConstraints.EAST;
	    gbc_btnthem.insets = new Insets(0, 0, 5, 0);
	    gbc_btnthem.gridx = 4;
	    gbc_btnthem.gridy = 0;
	    panelRight.add(btnthem, gbc_btnthem);
	    	    
	    scrollTblDis = new JScrollPane();
	    scrollTblDis.setViewportView(tblDis);
	    gbc_scrollTblDis = new GridBagConstraints();
	    gbc_scrollTblDis.insets = new Insets(0, 15, 0, 0);
	    gbc_scrollTblDis.fill = GridBagConstraints.BOTH;
	    gbc_scrollTblDis.gridwidth = 5;
	    gbc_scrollTblDis.gridx = 0;
	    gbc_scrollTblDis.gridy = 1;
	    panelRight.add(scrollTblDis, gbc_scrollTblDis);
	    //	Add Mouse Event	    
	    tblDis.addMouseListener(new MouseListDis());
	    splitPane.setRightComponent(panelRight);
	    /*--------------- Bang Quận Huyên cua tung Tỉnh Thành Phố -----------------*/
	}
    
    @Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnBack){
			if(pageCur == 1){
				return;
			}
			tblProV.setVisible(false);
			pageCur = pageCur - 1;
			cbbPage.setSelectedItem(pageCur);
			tblProV = UtilityGUI.getTable("Province", pageCur, "ProvinceId", true);
			tblProV.addMouseListener(new MouseListPr());
			scrollPane.setViewportView(tblProV);
		}else if(e.getSource() == btnNext){
			if(pageCur == cbbPage.getItemCount()){
				return;
			}
			tblProV.setVisible(false);
			pageCur = pageCur + 1;
			cbbPage.setSelectedItem(pageCur);
			tblProV = UtilityGUI.getTable("Province", pageCur, "ProvinceId", true);
			tblProV.addMouseListener(new MouseListPr());
			scrollPane.setViewportView(tblProV);
		}else if(e.getSource() == cbbPage){
			int pageGo = (Integer) cbbPage.getSelectedItem();
			if(pageCur == pageGo){
				return;
			}else{
				pageCur = pageGo;
				tblProV.setVisible(false);
				tblProV = UtilityGUI.getTable("Province", pageGo, "ProvinceId", true);
				tblProV.addMouseListener(new MouseListPr());
				scrollPane.setViewportView(tblProV);
			}
		}
	}
	
	public static class MouseListPr extends MouseAdapter{
	    private int prvId;
	   	private String prvName;
		public void mouseClicked(MouseEvent e) {
			 if((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
				 if(tblProV.getSelectedColumn() ==tblProV.getColumnCount()-2||tblProV.getSelectedColumn() ==tblProV.getColumnCount()-3){				
				 btnthem.setVisible(true);
				 int row = tblProV.getSelectedRow();
				 prvId =  (Integer) tblProV.getModel().getValueAt(row, 0);
				 prvName = (String) tblProV.getModel().getValueAt(row, 1);
				 lblTen.setVisible(false);
				 lblTen = new JLabel(prvName);
				 lblTen.setFont(UtilityGUI.fontHeader2);
				 lblTen.setForeground(UtilityGUI.colorHeader2);
				 panelRight.add(lblTen, gbc_lblId);			 
				 tblDis = UtilityGUI.createTblDistrict(prvId, 1, "DistrictId", true);
				 tblDis.addMouseListener(new MouseListDis()) ;	 
					 scrollTblDis.setVisible(false);
					 scrollTblDis = new JScrollPane();
					 scrollTblDis.setViewportView(tblDis);
					 panelRight.add(scrollTblDis, gbc_scrollTblDis);
				 }else if(tblProV.getSelectedColumn() == tblProV.getColumnCount()-1){
					 btnthem.setVisible(true);
					 int row = tblProV.getSelectedRow();
					 prvId =  (Integer) tblProV.getModel().getValueAt(row, 0);
					 prvName = (String) tblProV.getModel().getValueAt(row, 1);
					 lblTen.setVisible(false);
					 lblTen = new JLabel(prvName);
					 lblTen.setFont(UtilityGUI.fontHeader2);
					 lblTen.setForeground(UtilityGUI.colorHeader2);
					 panelRight.add(lblTen, gbc_lblId);			 
					 tblDis = UtilityGUI.createTblDistrict(prvId, 1, "DistrictId", true);
					 tblDis.addMouseListener(new MouseListDis()) ;	 
						 scrollTblDis.setVisible(false);
						 scrollTblDis = new JScrollPane();
						 scrollTblDis.setViewportView(tblDis);
						 panelRight.add(scrollTblDis, gbc_scrollTblDis);
					 prvName = (String) tblProV.getModel().getValueAt(tblProV.getSelectedRow(), 1);
					 int row1 =  tblProV.getSelectedRow();
			         int prvId1 = (Integer) tblProV.getModel().getValueAt(tblProV.getSelectedRow(), 0);
					 new Province_Update(null, prvName,prvId1,row1).setVisible(true);
				 }	
		}
	}
	 public static MouseListPr getMouse_tblPro(){
			return getMouse_tblPro();
		}
	 public static class MouseListDis extends MouseAdapter{
	    private int prvId;
	   	private String prvName;
		public void mouseClicked(MouseEvent e) {
			 if((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
				 if(tblDis.getSelectedColumn() == tblDis.getColumnCount()-1){
					 String disName = (String) tblDis.getModel().getValueAt(tblDis.getSelectedRow(), 1);
					 int row1 =  tblDis.getSelectedRow();
			         int disId1 = (Integer) tblDis.getModel().getValueAt(tblDis.getSelectedRow(), 0);
			         prvId =  (Integer) tblProV.getModel().getValueAt(tblProV.getSelectedRow(), 0);
			         new District_Update(null, disName,disId1,prvId,row1).setVisible(true);
				 }	
		}
	}
	 public static MouseListDis getMouse_tblDis(){
			return getMouse_tblDis();
		}

}




	
	
